package com.chengyu.core.service.member.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.UmsMemberAccountMapper;
import com.chengyu.core.mapper.UmsMemberThirdAccountMapper;
import com.chengyu.core.model.*;
import com.chengyu.core.service.member.MemberAccountService;
import com.chengyu.core.service.member.MemberThirdAccountService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author LeGreen
 * @title 会员绑定给第三方账号
 * @date 2021/1/6
 */
@Service("MemberThirdAccountServiceImpl")
public class MemberThirdAccountServiceImpl implements MemberThirdAccountService {

    @Autowired
    private UmsMemberThirdAccountMapper memberThirdAccountMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<UmsMemberThirdAccount> getList(Integer memberId, Integer type, Integer page, Integer pageSize) throws ServiceException {
        PageHelper.startPage(page, pageSize);
        UmsMemberThirdAccountExample example = new UmsMemberThirdAccountExample();
        UmsMemberThirdAccountExample.Criteria criteria = example.createCriteria();
        if (memberId != null) {
            criteria.andMemberIdEqualTo(memberId);
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        return memberThirdAccountMapper.selectByExample(example);
    }

    @Override
    public void delete(Integer id) {
        memberThirdAccountMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(UmsMemberThirdAccount umsMemberThirdAccount) {
        memberThirdAccountMapper.insertSelective(umsMemberThirdAccount);
    }

    @Override
    public List<UmsMemberThirdAccount> checkIsExist(UmsMemberThirdAccount umsMemberThirdAccount) {
        UmsMemberThirdAccountExample example = new UmsMemberThirdAccountExample();
        UmsMemberThirdAccountExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(umsMemberThirdAccount.getMemberId());
        criteria.andTypeEqualTo(umsMemberThirdAccount.getType());
        criteria.andUnionIdEqualTo(umsMemberThirdAccount.getUnionId());
        List<UmsMemberThirdAccount> umsMemberThirdAccounts = memberThirdAccountMapper.selectByExample(example);
        return umsMemberThirdAccounts;

    }

    @Override
    public List<UmsMemberThirdAccount> selectThirdAccountByMemberId(Integer memberId) {
        UmsMemberThirdAccountExample example = new UmsMemberThirdAccountExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<UmsMemberThirdAccount> umsMemberThirdAccounts = memberThirdAccountMapper.selectByExample(example);
        return umsMemberThirdAccounts;
    }

    @Override
    public List<UmsMemberThirdAccount> selectThirdAccountByOpenId(String openId) {
        UmsMemberThirdAccountExample example = new UmsMemberThirdAccountExample();
        example.createCriteria().andOpenIdEqualTo(openId);
        List<UmsMemberThirdAccount> umsMemberThirdAccounts = memberThirdAccountMapper.selectByExample(example);
        return umsMemberThirdAccounts;
    }

    @Override
    public void updateRefreshTokenByOpenId(String updateByOpenId,String refreshToken) {
        UmsMemberThirdAccountExample example = new UmsMemberThirdAccountExample();
        example.createCriteria().andOpenIdEqualTo(updateByOpenId);

        UmsMemberThirdAccount umsMemberThirdAccount = new UmsMemberThirdAccount();
        umsMemberThirdAccount.setRefreshToken(refreshToken);
        memberThirdAccountMapper.updateByExampleSelective(umsMemberThirdAccount, example);
    }
    @Override
    public void updateAccessTokenByOpenId(String updateByOpenId,String accessToken) {
        UmsMemberThirdAccountExample example = new UmsMemberThirdAccountExample();
        example.createCriteria().andOpenIdEqualTo(updateByOpenId);

        UmsMemberThirdAccount umsMemberThirdAccount = new UmsMemberThirdAccount();
        umsMemberThirdAccount.setAccessToken(accessToken);
        memberThirdAccountMapper.updateByExampleSelective(umsMemberThirdAccount, example);
    }

}
